exports.controllers={
/*****************************************************************************/
	admin_transaction_types: function(space) {
		if (!space.uid) return "-";
		if (!space.roles.transactions_settings) return "-";
		return space.controllers['admin_transaction_types__'+(space.fields.mode||"list")](space);
	},
	
	
	admin_transaction_types__list: function(space) {
		space.ttypes=space.site.sql.execute_and_fetch("transaction_types/list_for_editor");
		return space.views.process("widgets/admin_transaction_types/list",space);
	},

	
	admin_transaction_types__edit: function(space) {
		space.roles=space.site.sql.execute_and_fetch("roles/list");space.roles.unshift({id:"",name:""});
		space.datatypes=space.site.sql.execute_and_fetch("transaction_field_datatypes/list");
		if (space.fields.type_id) {
			space.ttype=space.site.sql.execute_and_fetch_one("transaction_types/get");
			space.states=space.site.sql.execute_and_fetch("transaction_states/list");
			space.field_groups=space.site.sql.execute_and_fetch("transaction_type_field_groups/list");
			space.field_groups.forEach(function(x) {
				x.fields=space.site.sql.execute_and_fetch("transaction_type_fields/list_for_editor",{group_id:x.id});
			});
		} else {
			space.ttype={};
			space.states=[];
			space.field_groups=[];
		}
		return space.views.process("widgets/admin_transaction_types/edit",space);
	},
	
	
	admin_transaction_types__save: function(space) {
		if (space.fields.type_id) {
			space.site.sql.execute("transaction_types/update");
		} else {
			space.site.sql.execute("transaction_types/insert");
			space.fields.type_id=space.site.sql.execute_and_fetch_one("transaction_types/max_id")['id'];
		}
		var cfields={};
		space.site.sql.execute_and_fetch("transaction_type_fields/list_of_type").forEach(function(x) {cfields[x.id]=1;});
		var cfield_groups={};
		space.site.sql.execute_and_fetch("transaction_type_field_groups/list").forEach(function(x) {cfield_groups[x.id]=1;});
		var cstates={};
		space.site.sql.execute_and_fetch("transaction_states/list").forEach(function(x) {cstates[x.id]=1;});
		var hashes=space.functions.parse_grids(space);
	
		for (var state_id in hashes.states) {
			var hash=hashes.states[state_id];
			if (state_id.match(/new/)) {
				space.site.sql.execute("transaction_states/insert",hash);
			} else {
				hash.state_id=state_id;
				space.site.sql.execute("transaction_states/update",hash);
				delete cstates[state_id];
			}
		}

		var fgs={};
		for (var field_group_id in hashes.fields) {
			var hash=hashes.fields[field_group_id];
			if (hash.rowClass!="field-group") continue;
			if (field_group_id.match(/new/)) {
				space.site.sql.execute("transaction_type_field_groups/insert",hash);
				fgs[field_group_id]=space.site.sql.execute_and_fetch_one("transaction_type_field_groups/max_id")['id'];
			} else {
				var fg_id=field_group_id.replace(/fg/,"");
				hash.group_id=fg_id;
				space.site.sql.execute("transaction_type_field_groups/update",hash);
				fgs[field_group_id]=fg_id;
				delete cfield_groups[fg_id];
			}
		}

		for (var field_id in hashes.fields) {
			var hash=hashes.fields[field_id];
			if (hash.rowClass=="field-group") continue;
			hash.group_id=fgs[hash.parent_id];
			if (field_id.match(/new/)) {
				space.site.sql.execute("transaction_type_fields/insert",hash);
			} else {
				hash.field_id=field_id;
				space.site.sql.execute("transaction_type_fields/update",hash);
				delete cfields[field_id];
			}
		}
		for (var state_id in cstates) space.site.sql.execute("transaction_states/delete",{state_id:state_id});
		for (var field_group_id in cfield_groups) space.site.sql.execute("transaction_type_field_groups/delete",{group_id:field_group_id});
		for (var field_id in cfields) space.site.sql.execute("transaction_type_fields/delete",{field_id:field_id});

		return JSON.stringify({redirect:{url:"/admin-transaction_types/"}});
	},

	admin_transaction_types__list_states:function(space) {
		return space.views.process("widgets/admin_transaction_types/list_states",space);
	}

};

